package com.mentaleye.biz.impl;

import com.mentaleye.bean.GroupBean;
import com.mentaleye.bean.OaClassBean;
import com.mentaleye.biz.GroupBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.GroupPersonService;
import com.mentaleye.service.GroupService;
import com.mentaleye.service.OaClassService;
import com.mentaleye.util.CommonResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Rock on 2017/5/8.
 */

@Service
public class GroupBizImpl implements GroupBiz{

    private static Logger logger = Logger.getLogger(GroupBizImpl.class);

    @Autowired private GroupService groupService;
    @Autowired private OaClassService oaClassService;
    @Autowired private GroupPersonService groupPersonService;

    public CommonResult edit(GroupBean bean){
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            bean.setGroupType(DictionaryEnums.STUDENT_GROUP.getCode());
            success = groupService.edit(bean);
            result.setIsSuccess(success);
        }catch (Exception e){
            logger.error("编辑分组异常",e);
        }
        return result;
    }

    public CommonResult remove(Integer groupId){
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            success = groupService.remove(groupId);
            if(success){
                groupPersonService.removeByGroupId(groupId);
            }
            result.setIsSuccess(success);
        }catch (Exception e){
            logger.error("删除分组异常",e);
        }
        return result;
    }

    public CommonResult removeByParentId(Integer groupId){
        CommonResult result = new CommonResult();
        boolean success = false;
        try{
            success = groupService.removeByParentId(groupId);
            result.setIsSuccess(success);
        }catch (Exception e){
            logger.error("根据父编号删除分组异常",e);
        }
        return result;
    }

    public GroupBean query(Integer groupId){
        try {
            return groupService.query(groupId);
        }catch (Exception e){
            logger.error("根据编号查询分组异常",e);
        }
        return null;
    }

    public List<GroupBean> queryByParentId(Integer parentId){
        try {
            return groupService.queryByParentId(parentId);
        }catch (Exception e){
            logger.error("根据父编号查询分组异常",e);
        }
        return null;
    }

    public List<GroupBean> queryByList(Integer groupType){
        try {
            List<GroupBean> groupList = groupService.queryList(groupType);
            List<OaClassBean> classList = oaClassService.queryAllOaClass();
            for (OaClassBean bean: classList) {
                GroupBean groupBean = new GroupBean();
                groupBean.setGroupId(bean.getOaclassId());
                groupBean.setGroupName(bean.getOaclassName());
                groupBean.setParentId(1);
                groupBean.setGroupType(DictionaryEnums.STUDENT_GROUP.getCode());
                groupBean.setGroupUrl("1;"+bean.getOaclassId()+";");
                if(groupList != null) {
                    groupList.add(groupBean);
                }
            }
            return groupList;
        }catch (Exception e){
            logger.error("根据分组类型查询分组异常",e);
        }
        return null;
    }
}
